/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: data_proxy_factory.hpp
 */

#ifndef _DATA_PROXY_FACTORY_HPP_
#define _DATA_PROXY_FACTORY_HPP_

#include <iostream>
#include <string>

#include "data_manager_proxy.hpp"

//-----------------------------------------------------------------------------
// Struct, enums
namespace middleware {
/*! \enum DataProxyDirection
 * Used to set DataProxyFactory by specifying communication mode.
 */
typedef enum {
	/*!
	 * When selected, it instructs the factory to build a DataManagerProxy
	 * ready to send Data to a particular Worker.
	 */
	kSendDataProxy = 0,
	/*!
	 * When selected, it instructs the factory to build a DataManagerProxy
	 * ready to receive Data from a particular Manager.
	 */
	kReceiveDataProxy = 1
} DataProxyDirection;
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*! \class DataProxyFactory
 * \brief Used to instantiate a DataManagerProxy object.
 *
 * Manages instantiation of a DataManagerProxy object.
 */
class DataProxyFactory {
public:
	/*!
	 * Constructs the factory specifying whether the final DataManagerProxy
	 * will be able to send or receive data to a Worker or from a Manager.
	 * \param dir Mode of factory, do we want a send-enabled DataManagerProxy,
	 * or a receive-enabled DataManagerProxy?
	 * \param ip_w Ip v4 address of the Worker to send Data to, or for Worker
	 * to bind itself.
	 * \param port_w TCP Port number of the Worker to send Data to, or for
	 * Worker to bind itself.
	 */
	DataProxyFactory(DataProxyDirection dir, std::string ip_w,
			std::string port_w);
	/*!
	 * Destroys factory. When factory encounters destructor, dynamic allocated
	 * memory for proxy is AUTOMATICALLY released; when a factory dies, its
	 * proxy is deleted. This happens also when factory is called to get
	 * a proxy more than one time.
	 */
	~DataProxyFactory();
	/*!
	 * Gets a well built DataManagerProxy: attention, a new instance is
	 * created via dynamic allocation. If a previous proxy was generated,
	 * it will be detroyed.
	 * \return Returns the pointer to the dynamically allocated DataManagerProxy.
	 */
	DataManagerProxy* getProxy();
private:
	/*!
	 * Holds built proxy. If the factory is detroyed, then so is the proxy.
	 */
	DataManagerProxy* _proxy;
	/*!
	 * Ip address to send data to.
	 */
	std::string _ip_to_w;
	/*!
	 * Port to send request to.
	 */
	std::string _port_to_w;
	/*!
	 * Ip address to bind and receive data.
	 */
	std::string _ip_bind_w;
	/*!
	 * Port address to bind and receive data.
	 */
	std::string _port_bind_w;
}; /* DataProxyFactory */
} /* middleware */

#endif
